package com.dtd.market.common;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	
	private static Logger logger = LoggerFactory.getLogger(BaseAction.class);
	
	public static final String RELOAD = "reload";
	public static final String DETAIL = "detail";
	public static final String EDIT = "edit";
	public static final String INDEX = "index";
	public static final String SUCCESS = "success";
	public static final String FAILURE = "failure";
	
	// header 常量定义
	private static final String ENCODING_PREFIX = "encoding";
	private static final String NOCACHE_PREFIX = "no-cache";
	private static final String ENCODING_DEFAULT = "UTF-8";
	private static final boolean NOCACHE_DEFAULT = true;
	
	public String execute() throws Exception {
		return INDEX;
	}
	
	public PageContext getContext() {
		return ServletActionContext.getPageContext();
	}
	
	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}
	
	public UserInfo getLoginUser() {
		return (UserInfo) getSession().getAttribute("userInfo");
	}
	
	public void setLoginUser(UserInfo user) {
		getSession().setAttribute("userInfo", user);
	}
	
	public void renderJson(final String string, final String... headers) {
		render("application/json", string, headers);
	}
	
	public void renderText(final String text, final String... headers) {
		render("text/plain", text, headers);
	}
	
	/**
	 * 直接输出内容的简便函数.
	 * 
	 * @param headers
	 *     可变的header数组，目前接受的值为"encoding:"或"no-cache:",
	 *     默认值分别为UTF-8和true.
	 */
	public static void render(final String contentType, final String content,
			final String... headers) {
		try {
			// 分析headers参数
			String encoding = ENCODING_DEFAULT;
			boolean noCache = NOCACHE_DEFAULT;
			for (String header : headers) {
				String headerName = StringUtils.substringBefore(header, ":");
				String headerValue = StringUtils.substringAfter(header, ":");

				if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
					encoding = headerValue;
				} else if (StringUtils.equalsIgnoreCase(headerName,
						NOCACHE_PREFIX)) {
					noCache = Boolean.parseBoolean(headerValue);
				} else {
					throw new IllegalArgumentException(headerName
							+ "不是一个合法的header类型");
				}
			}

			HttpServletResponse response = ServletActionContext.getResponse();

			// 设置headers参数
			String fullContentType = contentType + ";charset=" + encoding;
			response.setContentType(fullContentType);
			if (noCache) {
				response.setHeader("Pragma", "No-cache");
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
			}

			response.getWriter().write(content);
			response.getWriter().flush();

		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
	
}
